/**
 * Package:com.goldcitynet.util;
 * $Id: Configure.java,v 1.6 2009/07/28 04:05:39 zhangpei Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.util;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Attribute;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;


/**
 * Configure类，读取配置文件goldcitynet-configure.xml的配置并保存到具体属性中
 * 这些属性需要在此处添加
 * <p/>
 * <p/>
 * <p/>
 * <p><a href="Configure.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: zhangpei $</a>
 * @version $Revision: 1.6 $
 */
public class Configure {
    private static Log log = LogFactory.getLog(Configure.class);
    private static Configure configure;

    public static final Integer SOURCE_TYPE_XML = 0;
    public static final Integer SOURCE_TYPE_PRO = 1;


    /* 所需配置参数在此处添加 还需配套添加getter和setter */
    private String uploadPath = "";
    private String accessPath = "";
    private String exchangeId = "";
    private String rootPath = "";
    private String mapeditorversion = "";
    private String defaultSceneId = "";//默认场景ID
    private String emptySceneId = "";//默认空地场景ID
    private String staticxml = "";
    private String url = "";
    private List<Map<String, String>> staticHTMLSource = new ArrayList<Map<String, String>>();
    private Map<String, String> visitorAppearances = new HashMap<String, String>();
    private Boolean devModel;


    private Configure() {
    }

    private Configure(String path, Integer type) {
        buildConfiguration(path, type);
    }

    public static Configure configure() {
        configure("configure_engine.xml");
        return configure;
    }

    public static Configure configure(String xmlPath) {
        configure(xmlPath, Configure.SOURCE_TYPE_XML);
        return configure;
    } 

    public static Configure configure(String path, Integer type) {
        if (configure == null) configure = new Configure(path, type);
        return configure;
    }

    public void reset() {
        this.uploadPath = "";
        this.accessPath = "";
        this.exchangeId = "";
        configure = null;
    }

    /* getter and setter begin*/
    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public List<Map<String, String>> getStaticHTMLSource() {
        return staticHTMLSource;
    }

    public void setStaticHTMLSource(List<Map<String, String>> staticHTMLSource) {
        this.staticHTMLSource = staticHTMLSource;
    }

    public String getAccessPath() {
        return accessPath;
    }

    public void setAccessPath(String accessPath) {
        this.accessPath = accessPath;
    }

    public String getExchangeId() {
        return exchangeId;
    }

    public void setExchangeId(String exchangeId) {
        this.exchangeId = exchangeId;
    }
    /* getter and setter over*/

    public String getDefaultSceneId() {
        return defaultSceneId;
    }

    public void setDefaultSceneId(String defaultSceneId) {
        this.defaultSceneId = defaultSceneId;
    }

    public String getEmptySceneId() {
        return emptySceneId;
    }

    public void setEmptySceneId(String emptySceneId) {
        this.emptySceneId = emptySceneId;
    }

    public String getRootPath() {
        return rootPath;
    }

    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
    }

    public Map<String, String> getVisitorAppearances() {
        return visitorAppearances;
    }

    public void setVisitorAppearances(Map<String, String> visitorAppearances) {
        this.visitorAppearances = visitorAppearances;
    }

    public String getStaticxml() {
        return staticxml;
    }

    public void setStaticxml(String staticxml) {
        this.staticxml = staticxml;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }/* 若有其它文件类型，需在此添加处理代码 */

    private void buildConfiguration(String path, Integer type) {
        try {
            if (type.equals(Configure.SOURCE_TYPE_XML)) {
                Document doc = ConfigHelper.getConfigDocument(path);
                addPropertiesToConfiguration(doc);
            }
            if (type.equals(Configure.SOURCE_TYPE_PRO)) {
                Properties properties = ConfigHelper.getConfigProperties(path);
                addPropertiesToConfiguration(properties);
            }
        } catch (Exception e) {
            log.error("创建系统配置文件对象失败！");
            e.printStackTrace();
        }
    }

    private void addPropertiesToConfiguration(Document doc) {
        Element root = doc.getRootElement();
        Iterator it = root.elementIterator();
        while (it.hasNext()) {
            Element child = (Element) it.next();
            String name = child.getName();
            //设置上传文件属性
            if (name.equals("upload")) {
                Element uploadPath = child.element("path");
                if (uploadPath != null)
                    setUploadPath(uploadPath.getTextTrim());
            }
            if (name.equals("access")) {
                Element accessPath = child.element("path");
                if (accessPath != null)
                    setAccessPath(accessPath.getTextTrim());
            }
            if (name.equals("root")) {
                Element rootPath = child.element("path");
                if (rootPath != null)
                    setRootPath(rootPath.getTextTrim());
            }
            if (name.equals("mapeditorversion")) {
                Element rootPath = child.element("path");
                if (rootPath != null)
                    setMapeditorversion(rootPath.getTextTrim());
            }
            //设置场景属性
            if (name.equals("scene")) {
                //默认场景
                Element rootPath = child.element("defaultsceneid");
                if (rootPath != null)
                    setDefaultSceneId(rootPath.getTextTrim());
                //默认空地场景
                rootPath = child.element("emptysceneid");
                if (rootPath != null)
                    setEmptySceneId(rootPath.getTextTrim());
                rootPath = child.element("path");
            }
            if (name.equalsIgnoreCase("staticxml")) {
                Element rootPath = child.element("realPath");
                if (rootPath != null)
                    setStaticxml(rootPath.getTextTrim());
                rootPath = child.element("url");
                if (rootPath != null)
                    setUrl(rootPath.getTextTrim());
            }

            if (name.equalsIgnoreCase("visitor")) {
                Iterator staticIt = child.elementIterator();
                while (staticIt.hasNext()) {
                    Element bn = (Element) staticIt.next();
                    String type = bn.selectSingleNode("type").getText();
                    String id = bn.selectSingleNode("id").getText();
                    visitorAppearances.put(type, id);
                }

            }
            //设置静态化页面配置
            if (name.equals("static")) {
                Iterator staticIt = child.elementIterator();
                while (staticIt.hasNext()) {
                    Element bn = (Element) staticIt.next();
                    Map<String, String> url = new HashMap<String, String>();
                    Iterator bnAttIt = bn.attributeIterator();
                    while (bnAttIt.hasNext()) {
                        Attribute att = (Attribute) bnAttIt.next();
                        url.put(att.getName(), att.getValue());
                    }
                    Iterator urlIt = bn.elementIterator();
                    while (urlIt.hasNext()) {
                        Element bnChild = (Element) urlIt.next();
                        url.put(bnChild.getName(), bnChild.getTextTrim());
                    }
                    staticHTMLSource.add(url);
                }
            }
            //设置充值参数
            if (name.equals("exchange")) {
                Iterator staticIt = child.elementIterator();
                while (staticIt.hasNext()) {
                    Element bn = (Element) staticIt.next();
                    if (bn.getName().equals("id")) {
                        exchangeId = bn.getTextTrim();
                    }
                }
            }
            /*其它属性在此添加*/

        }
    }

    private void addPropertiesToConfiguration(Properties properties) {

    }

    public static void main(String[] args) {

    }

    public String getMapeditorversion() {
        return mapeditorversion;
    }

    public void setMapeditorversion(String mapeditorversion) {
        this.mapeditorversion = mapeditorversion;
    }

}
